<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class TradeDetail extends Model
{
    public $table = 'tradedetail';

    public $primaryKey = 'id_tradedetail';

    public $guarded = [];

    public $timestamps = false;

    public function wxuser()
    {
        return $this->hasOne(Wxuser::class,'id_wxuser','id_wxuser');
    }

    public function getFormat1CDate($value)
    {
        $now = time();
        $createtime = strtotime($this->createtime);

        $diff = $now - $createtime;
        $diff = $diff < 0? 0 : $diff;

        if($diff < 60){
            return intval($diff) . '秒前';
        }else if($diff < 60*60){
            return intval($diff/60) . '分钟前';
        }else if($diff < 60*60*24){
            return intval($diff/60/60) . '小时前';
        }else{
            return intval($diff/60/60/24) . '天前';
        }
    }

    public function getPaytimeAttribute($value)
    {
        $now = time();
        $createtime = strtotime($value);

        if(date('Y', $now) == date('Y', $createtime)){
            if(date('Y-m-d', strtotime('-1 day')) == date('Y-m-d', $createtime)){
                return '昨天' . date('H:i', $createtime);
            }else if(date('Y-m-d', $now) == date('Y-m-d', $createtime)){
                return date('今天H:i:s', $createtime);
            }else{
                //return date('m月d日 H:i', $createtime);
                return date('m月d日 H:i', $createtime);
            }
        }else{
            //return date('Y年m月d日 H:i', $createtime);
            return date('Y年m月d日  H:i', $createtime);
        }
    }

    public function getAmountDueAttribute($value)
    {
        return round($value/100,2);
    }
}
